package vn.bus.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import vn.bus.dto.ConnectionDTO;

public class ConnectionDAO {

	public ArrayList<ConnectionDTO> getAllConnections(Connection connection) throws Exception
	{
		ArrayList<ConnectionDTO> allConns = new ArrayList<ConnectionDTO>();
		try
		{
			PreparedStatement ps = connection.prepareStatement("SELECT * FROM connections");
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				ConnectionDTO Conn = new ConnectionDTO();
				Conn.setDistance(rs.getInt("distance"));
				Conn.setId(rs.getInt("id"));
				Conn.setNext(rs.getString("next"));
				Conn.setOrder(rs.getInt("order"));
				Conn.setRoute(rs.getInt("route"));
				Conn.setStation(rs.getString("station"));
				Conn.setTurn(rs.getInt("turn"));
				Conn.setPolyline(rs.getString("polyline"));
				allConns.add(Conn);
			}
			return allConns;
		}
		catch(Exception e)
		{
			throw e;
		}
	}
	
	public String updatePolyline(Connection connection, String polyline, String station, String next) throws Exception
	{
		String rsStr = "";
		try
		{
			String rq = "UPDATE connections SET `polyline`=\"" + polyline + "\" WHERE `station`=\"" + station
					+ "\" AND `next`=\"" + next + "\"";
			PreparedStatement ps = connection.prepareStatement(rq);
			ps.executeUpdate();
			rsStr = "OK";
			System.out.println("UpdateConnPolyline : " + polyline + " -> successful");
		}
		catch(Exception e)
		{
			throw e;
		}
		return rsStr;
	}
}
